package com.link.play.fit.ai.ui.dialog

import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.text.method.DialerKeyListener
import android.view.Gravity
import android.view.KeyEvent
import android.view.WindowManager
import android.widget.ImageView
import com.link.play.fit.ai.R
import com.link.play.fit.ai.databinding.DialogFoodDatabaseAddFoodBinding

//添加食物弹窗
class FoodDatabaseDialog(var openEditFood: () -> Unit) {
    private var dialog: Dialog? = null


    //    fun build(context: Context) {
//        val iv = ImageView(context)
//        val wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
//        val param = WindowManager.LayoutParams()
//        param.x = 100
//        param.y = 200
//        wm.addView(iv, param)
//    }
    fun build(context: Activity) {
        val binding = DialogFoodDatabaseAddFoodBinding.inflate(
            context.layoutInflater
        )
        dialog = Dialog(context, R.style.AlertDialogStyle)
        dialog!!.setContentView(binding.root)
        val window = dialog!!.window
        if (window == null) return
        window.decorView.setPadding(0, 0, 0, 0)
        val lp = window.attributes
        lp.width = WindowManager.LayoutParams.MATCH_PARENT
        lp.height = WindowManager.LayoutParams.MATCH_PARENT
        window.setGravity(Gravity.CENTER)
        window.attributes = lp
        binding.btnView.setOnClickListener {
            openEditFood.invoke()
            dialog?.apply { dismiss() }
        }
        binding.constraint.setOnClickListener {
        }
        binding.root.setOnClickListener {
            dialog?.apply { dismiss() }
        }
        dialog?.apply {
            setOnKeyListener { a, b, c ->
                if (b == KeyEvent.KEYCODE_BACK && c.action == KeyEvent.ACTION_UP) {
                    a.dismiss()
                    true
                } else {
                    false
                }
            }
        }
    }

    fun show() {
        dialog?.apply {
            show()
        }
    }
    fun dismiss(){
        dialog?.apply {
            dismiss()
        }
    }
}